package org.idea.qiyu.red.packet.service;

import org.apache.dubbo.config.annotation.DubboReference;
import org.idea.qiyu.pay.service.IPayService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;

/**
 * 批量通知第三方服务发送红包金额到指定用户账户中
 *
 * @Author linhao
 * @Date created in 9:22 上午 2021/11/14
 */
@Service
public class InitBatchSendRedPacketService implements CommandLineRunner {

    @DubboReference
    private IPayService iPayService;

    public static BlockingQueue<String> blockingQueue = new ArrayBlockingQueue(100000);

    @Override
    public void run(String... args) throws Exception {
        Thread thread = new Thread(new BatchSendRedPacketTask());
        thread.start();
    }
    private class BatchSendRedPacketTask implements Runnable{

        @Override
        public void run() {
            while (true) {
                String info = blockingQueue.poll();
                if(StringUtils.isEmpty(info)){
                    continue;
                }
                String infos[] = info.split(";");
                int money = Integer.parseInt(infos[0]);
                long userId = Long.valueOf(infos[1]);
                iPayService.sendMoneyToUser(userId,money);
            }
        }
    }

}
